//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Common Core Extensions
//===============================================================================

using System.IO;

namespace EntLibContrib.Common.Configuration.Design
{
	/// <summary>
	/// Xml resource name converter.
	/// </summary>
	internal class XmlResourceNameConverter : FileResourceNameConverter
	{
		#region Public Methods
		/// <summary>
		/// Converts the given object to the type of this converter, using the specified context and culture information.
		/// </summary>
		/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
		/// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"/> to use as the current culture.</param>
		/// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
		/// <returns>
		/// An <see cref="T:System.Object"/> that represents the converted value.
		/// </returns>
		/// <exception cref="T:System.NotSupportedException">
		/// The conversion cannot be performed.
		/// </exception>
		public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			string fileResourceName = value as string;

			if (fileResourceName != null)
				return new XmlResourceName(Path.GetDirectoryName(fileResourceName), Path.GetFileNameWithoutExtension(fileResourceName));

			return base.ConvertFrom(context, culture, value);
		}

		/// <summary>
		/// Finds the file resource name extension.
		/// </summary>
		/// <returns>extension name with separator</returns>
		public override string FindFileResourceNameExtension()
		{
			return CommonSettings.XmlExtension;
		}
		#endregion
	}
}
